package org.dromara.resource.api.services;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.AssertUtils;
import org.dromara.resource.api.domain.RemoteFile;
import org.dromara.resource.api.feign.ResourceFileClient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: zhou shuai
 * @date: 2024/10/6 16:43
 * @version: v1
 */
@Slf4j
@Service("resourceFileClientApiService")
@RequiredArgsConstructor
public class ResourceFileClientApiService {

    private final ResourceFileClient resourceFileClient;

    /**
     * 文件上传请求
     *
     * @author: zhou shuai
     * @date: 2024/10/6 16:44
     * @param: avatarfile
     * @return: org.dromara.resource.api.domain.RemoteFile
     */
    public RemoteFile fileUpload(MultipartFile avatarfile) {
        R<RemoteFile> fileUploadResult = resourceFileClient.fileUpload(avatarfile);
        AssertUtils.assertTrue(R.isSuccess(fileUploadResult), "上传文件失败");
        return fileUploadResult.getData();
    }

    /**
     * 通过ossId查询对应的url
     *
     * @param ossIds ossId串逗号分隔
     * @return url串逗号分隔
     */
    public String getFileUrlByOssIds(String ossIds) {
        R<String> fileUrlResult = resourceFileClient.getFileUrlByOssIds(ossIds);
        AssertUtils.assertTrue(R.isSuccess(fileUrlResult), "获取文件URL失败");
        return fileUrlResult.getData();
    }

    /**
     * 通过ossId查询列表
     *
     * @param ossIds ossId串逗号分隔
     * @return 列表
     */
    public List<RemoteFile> getFileListByOssIds(String ossIds) {
        R<List<RemoteFile>> fileListResult = resourceFileClient.getFileListByOssIds(ossIds);
        AssertUtils.assertTrue(R.isSuccess(fileListResult), "获取文件列表失败");
        return fileListResult.getData();
    }

}
